<template>
  <t-link
    v-permission="['system:order:apply-delete']"
    theme="primary"
    @click="handleOpenDialog('update', { orderId: props.orderId, approveType: props.approveType })"
  >
    申请废单
  </t-link>
  <!-- 自定义对话框 -->
  <custom-dialog
    ref="customDialogRef"
    v-slot="{ formData }"
    :api-map="{ update: applyAbandonedOrder }"
    header="申请废单"
    @request-success="handleDialogRequestSuccess"
  >
    <t-form-item label="审批类型" name="type">
      {{ dictionaryStore.analysis('order_approval_type', props.approveType) }}
    </t-form-item>
    <t-form-item
      label="申请备注"
      name="extensionRemark"
      :rules="[{ required: true, message: '请输入申请备注' }]"
    >
      <t-textarea v-model:value="formData.extensionRemark" placeholder="请输入申请备注" />
    </t-form-item>
  </custom-dialog>
</template>

<script setup lang="ts">
import { MessagePlugin } from 'tdesign-vue-next';
import { PropType, ref, useTemplateRef } from 'vue';

import {
  applyAbandonedOrder,
} from '@/api/approve/approve';
import { getUserListByCompanyIdAndRoleId } from '@/api/system/user';
import CustomDialog from '@/components/custom/dialog/index.vue';
import { useDictionaryStore, useUserStore } from '@/store';

// 字典仓库
const dictionaryStore = useDictionaryStore();
// props
const props = defineProps({
  // 订单id
  orderId: {
    type: Number,
    required: true,
  },
  // 审批类型
  approveType: {
    type: String,
    required: true,
  },
});
// emits
const emits = defineEmits(['requestSuccess']);

// 自定义对话框ref
const customDialogRef = useTemplateRef('customDialogRef');
// 处理打开对话框
const handleOpenDialog = (mode: 'create' | 'read' | 'update', params: Record<string, any>) => {
  customDialogRef.value.open(mode, params);
};
// 处理对话框请求成功
const handleDialogRequestSuccess = (result: Record<string, any>) => {
  emits('requestSuccess', result);
};
</script>
